//
//  SpiderWeb.m
//  StarBugs
//
//  Created by ?? ? on 7/3/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Object/SpiderWeb.h"
#include "Util/ModuleController.h"
using namespace cocos2d;

SpiderWeb::~SpiderWeb()
{
    
}

SpiderWeb::SpiderWeb()
{
    mainSprite = new CCSprite;
    mainSprite->autorelease();
    mainSprite->init();
    addChild(mainSprite);
    
//    centerPoint = CCPointZero;
    schedule(schedule_selector(SpiderWeb::tick));
    objectType = objectType_spiderWeb;
    useTimer = 0;
    isShot = false;
}

void SpiderWeb::tick(float dt)
{
    if(useTimer > 0)
    {
        useTimer -= dt;
        useTimer = MAX(useTimer, 0);
    }
}

void SpiderWeb::draw()
{
    linePoint[0] = ccp(-7 * ModuleController::getImageScale(), 23 * ModuleController::getImageScale());
    linePoint[1] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[2] = ccp(-24 * ModuleController::getImageScale(), 14 * ModuleController::getImageScale());
    linePoint[3] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[4] = ccp(-23 * ModuleController::getImageScale(), -6 * ModuleController::getImageScale());
    linePoint[5] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[6] = ccp(-15 * ModuleController::getImageScale(), -25 * ModuleController::getImageScale());
    linePoint[7] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[8] = ccp(7 * ModuleController::getImageScale(), -23 * ModuleController::getImageScale());
    linePoint[9] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[10] = ccp(26 * ModuleController::getImageScale(), -15 * ModuleController::getImageScale());
    linePoint[11] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[12] = ccp(25 * ModuleController::getImageScale(), 7 * ModuleController::getImageScale());
    linePoint[13] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[14] = ccp(14 * ModuleController::getImageScale(), 26 * ModuleController::getImageScale());
    linePoint[15] = ccp(mainSprite->getPosition().x - getPosition().x, mainSprite->getPosition().y - getPosition().y);
    
    linePoint[16] = ccp(linePoint[10].x + (linePoint[11].x - linePoint[10].x) * 0.22, linePoint[10].y + (linePoint[11].y - linePoint[10].y) * 0.22);
    linePoint[17] = ccp(linePoint[12].x + (linePoint[13].x - linePoint[12].x) * 0.06, linePoint[12].y + (linePoint[13].y - linePoint[12].y) * 0.06);
    linePoint[18] = ccp(linePoint[14].x + (linePoint[15].x - linePoint[14].x) * 0.21, linePoint[14].y + (linePoint[15].y - linePoint[14].y) * 0.21);
    
    linePoint[19] = ccp(linePoint[0].x + (linePoint[1].x - linePoint[0].x) * 0.09, linePoint[0].y + (linePoint[1].y - linePoint[0].y) * 0.09);
    linePoint[20] = ccp(linePoint[2].x + (linePoint[3].x - linePoint[2].x) * 0.23, linePoint[2].y + (linePoint[3].y - linePoint[2].y) * 0.23);
    linePoint[21] = ccp(linePoint[4].x + (linePoint[5].x - linePoint[4].x) * 0.07, linePoint[4].y + (linePoint[5].y - linePoint[4].y) * 0.07);
    linePoint[22] = ccp(linePoint[6].x + (linePoint[7].x - linePoint[6].x) * 0.26, linePoint[6].y + (linePoint[7].y - linePoint[6].y) * 0.26);
    linePoint[23] = ccp(linePoint[8].x + (linePoint[9].x - linePoint[8].x) * 0.17, linePoint[8].y + (linePoint[9].y - linePoint[8].y) * 0.17);
    
    linePoint[24] = ccp(linePoint[10].x + (linePoint[11].x - linePoint[10].x) * 0.37, linePoint[10].y + (linePoint[11].y - linePoint[10].y) * 0.37);
    linePoint[25] = ccp(linePoint[12].x + (linePoint[13].x - linePoint[12].x) * 0.32, linePoint[12].y + (linePoint[13].y - linePoint[12].y) * 0.32);
    linePoint[26] = ccp(linePoint[14].x + (linePoint[15].x - linePoint[14].x) * 0.48, linePoint[14].y + (linePoint[15].y - linePoint[14].y) * 0.48);
    linePoint[27] = ccp(linePoint[0].x + (linePoint[1].x - linePoint[0].x) * 0.45, linePoint[0].y + (linePoint[1].y - linePoint[0].y) * 0.45);
    linePoint[28] = ccp(linePoint[2].x + (linePoint[3].x - linePoint[2].x) * 0.6, linePoint[2].y + (linePoint[3].y - linePoint[2].y) * 0.6);
    
    linePoint[29] = ccp(linePoint[4].x + (linePoint[5].x - linePoint[4].x) * 0.51, linePoint[4].y + (linePoint[5].y - linePoint[4].y) * 0.51);
    linePoint[30] = ccp(linePoint[6].x + (linePoint[7].x - linePoint[6].x) * 0.62, linePoint[6].y + (linePoint[7].y - linePoint[6].y) * 0.62);
    linePoint[31] = ccp(linePoint[8].x + (linePoint[9].x - linePoint[8].x) * 0.59, linePoint[8].y + (linePoint[9].y - linePoint[8].y) * 0.59);
    linePoint[32] = ccp(linePoint[10].x + (linePoint[11].x - linePoint[10].x) * 0.7, linePoint[10].y + (linePoint[11].y - linePoint[10].y) * 0.7);
    linePoint[33] = ccp(linePoint[12].x + (linePoint[13].x - linePoint[12].x) * 0.72, linePoint[12].y + (linePoint[13].y - linePoint[12].y) * 0.72);
    
    linePoint[34] = ccp(linePoint[14].x + (linePoint[15].x - linePoint[14].x) * 0.77, linePoint[14].y + (linePoint[15].y - linePoint[14].y) * 0.77);
    linePoint[35] = ccp(linePoint[0].x + (linePoint[1].x - linePoint[0].x) * 0.75, linePoint[0].y + (linePoint[1].y - linePoint[0].y) * 0.75);
    
    
//    glDisable(GL_TEXTURE_2D);
//	glDisableClientState(GL_COLOR_ARRAY);
//	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

//    glColor4ub(230, 230, 230, 255);
    ccDrawColor4B(230, 230, 230, 255);

    for(int i = 0; i < 8; i++)
    {
//        ccDrawSmoothLine(linePoint[i * 2], linePoint[i * 2 +1], 0.2 * ModuleController::getImageScale());
        ccDrawLine(linePoint[i * 2], linePoint[i * 2 +1]);
    }
    for(int i = 0; i < 19; i++)
    {
//        ccDrawSmoothLine(linePoint[16 + i], linePoint[17 + i], 0.2 * ModuleController::getImageScale());
        ccDrawLine(linePoint[16 + i], linePoint[17 + i]);
    }

//    glDisable(GL_BLEND);
//	glEnable(GL_TEXTURE_2D);
//	glEnableClientState(GL_COLOR_ARRAY);
//	glEnableClientState(GL_TEXTURE_COORD_ARRAY);	
}

void SpiderWeb::setBody(b2World *_world)
{
    mainSprite->setPosition(position);
    mainSprite->setRotation(bodyAngle);
    
    setPosition(position);
}

//void SpiderWeb::removeBody(b2World *_world)
//{
//    
//}
//
//void SpiderWeb::setBodyAngle(bool isInv)
//{
//    
//}

void SpiderWeb::setMove(cocos2d::CCPoint pt)
{
    position = pt;
    mainSprite->setPosition(position);
    setPosition(position);
}

void SpiderWeb::setCaught()
{
    CCMoveBy *move = CCMoveBy::create(0.05, ccp(2, 3));
    CCMoveBy *move2 = CCMoveBy::create(0.05, ccp(-3, -5));
    CCMoveBy *move3 = CCMoveBy::create(0.05, ccp(2, -2));
    CCMoveTo *move4 = CCMoveTo::create(0.05, getPosition());
    mainSprite->runAction(CCSequence::create(move, move2, move3, move4, NULL));
}
void SpiderWeb::setAim(cocos2d::CCPoint pt)
{
    mainSprite->setPosition(pt);
    isShot = false;
}

void SpiderWeb::shot()
{
    CCMoveTo *move = CCMoveTo::create(0.1, getPosition());
    CCCallFunc *call = CCCallFunc::create(this, callfunc_selector(SpiderWeb::call_shot));
    CCMoveBy *move1 = CCMoveBy::create(0.05, ccp(2, 3));
    CCMoveBy *move2 = CCMoveBy::create(0.05, ccp(-3, -5));
    CCMoveBy *move3 = CCMoveBy::create(0.05, ccp(2, -2));
    CCMoveTo *move4 = CCMoveTo::create(0.05, getPosition());
    
    mainSprite->runAction(CCSequence::create(move, call, move1, move2, move3, move4, NULL));
    useTimer = 0.3;
}


void SpiderWeb::setInitPos()
{
    CCMoveTo *move = CCMoveTo::create(0.08, getPosition());
    
    CCMoveBy *move1 = CCMoveBy::create(0.05, ccp(2, 3));
    CCMoveBy *move2 = CCMoveBy::create(0.05, ccp(-3, -5));
    CCMoveBy *move3 = CCMoveBy::create(0.05, ccp(2, -2));
    CCMoveTo *move4 = CCMoveTo::create(0.05, getPosition());
    mainSprite->runAction(CCSequence::create(move, move1, move2, move3, move4, NULL));
}

void SpiderWeb::call_shot()
{
    isShot = true;
}